﻿namespace Homework15
{
    internal class Program
    {
        /// <summary>
        /// output canlendar for this month
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //output today in the first line
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Blue;
            //yyyy代表年，MM代表月，dd代表今天，小时H，分钟m
            Console.WriteLine(DateTime.Today.ToString("yyyy年MM月dd日"));
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(new String('_', 40));
            Console.WriteLine("一   二  三  四  五  六  日");
            //get first day of this month
            DateTime FirstDayOfThisMonth = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            DateTime FirstDayOfFirstWeek = FirstDayOfThisMonth.AddDays(1 - (int)FirstDayOfThisMonth.DayOfWeek);
            //gets last day of This month 
            DateTime lastDayOfThisMonth = FirstDayOfThisMonth.AddMonths(1).AddDays(-1);
            //get weekdayOflastday如果是周日就是最后一天，否则还得取所在周的周日
            //周日=0
            int weekdayOfLastDayOfThisMonth = (int)lastDayOfThisMonth.DayOfWeek;
            DateTime lastDayOfLastweek;
            if (weekdayOfLastDayOfThisMonth == 0)
            {
                //本月最后一天是周日
                lastDayOfLastweek = lastDayOfThisMonth;
            }
            else
            {
                lastDayOfLastweek = lastDayOfThisMonth.AddDays(7 - weekdayOfLastDayOfThisMonth);
            }
            //
            for (DateTime ThisDay = FirstDayOfFirstWeek; ThisDay <= lastDayOfLastweek; ThisDay = ThisDay.AddDays(1))
            {
                if (ThisDay.Month == DateTime.Today.Month && ThisDay.Day == DateTime.Today.Day)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.BackgroundColor = ConsoleColor.Red;
                }
                if (ThisDay.Day < 10)
                    Console.Write(" ");
                Console.Write(ThisDay.Day.ToString(""));
                Console.ForegroundColor = ConsoleColor.White;
                Console.BackgroundColor = ConsoleColor.Black;
                Console.Write("  ");
                if ((int)ThisDay.DayOfWeek == 0)
                    Console.WriteLine();
            }
        }
    }
}